// NodeListPane.cpp : implementation file
//

#include "stdafx.h"
#include "WebGUI.h"
#include "NodeListPane.h"


// CNodeListPane

IMPLEMENT_DYNAMIC(CNodeListPane, CDockablePane)

CNodeListPane::CNodeListPane()
{

}

CNodeListPane::~CNodeListPane()
{
	m_Font.DeleteObject() ;

}


BEGIN_MESSAGE_MAP(CNodeListPane, CDockablePane)
	ON_WM_PAINT()
	ON_WM_SETFOCUS()
	ON_WM_SIZE()
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_ERASEBKGND()
END_MESSAGE_MAP()

void CNodeListPane::OnPaint()
{
	CPaintDC dc(this); // device context for painting

	CRect rectTree;
	m_ctrlNodeList.GetWindowRect(rectTree);
	ScreenToClient(rectTree);

	rectTree.InflateRect(1, 1);
	dc.Draw3dRect(rectTree, ::GetSysColor(COLOR_3DSHADOW), ::GetSysColor(COLOR_3DSHADOW));
}

void CNodeListPane::OnSetFocus(CWnd* pOldWnd)
{
	CDockablePane::OnSetFocus(pOldWnd);
	m_ctrlNodeList.SetFocus();
}

void CNodeListPane::OnSize(UINT nType, int cx, int cy)
{
	CDockablePane::OnSize(nType, cx, cy);

	if (CanAdjustLayout())
	{
		CRect rc;
		GetClientRect(rc);

		m_ctrlNodeList.SetWindowPos(NULL, rc.left+0 , rc.top+0 , rc.Width()-0 , rc.Height()-0 , SWP_NOACTIVATE | SWP_NOZORDER );

		CRect rcList ;
		m_ctrlNodeList.GetClientRect ( rcList ) ;
	}
}

int CNodeListPane::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDockablePane::OnCreate(lpCreateStruct) == -1)
		return -1;

	m_Font.CreateStockObject(DEFAULT_GUI_FONT);

	CRect rectDummy;
	rectDummy.SetRectEmpty();

	// Create output pane:
	const DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_VSCROLL | LVS_REPORT | WS_BORDER ;

	m_ctrlNodeList.Create(dwStyle, rectDummy, this, 1);
	m_ctrlNodeList.SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
	m_ctrlNodeList.InsertColumn(1, _T("IP"), LVCFMT_LEFT, 120);
	m_ctrlNodeList.InsertColumn(2, _T("Type"), LVCFMT_LEFT, 120);
	m_ctrlNodeList.InsertColumn(3, _T("CPU Load"), LVCFMT_LEFT, 100);
	m_ctrlNodeList.InsertColumn(4, _T("Mem Used"), LVCFMT_LEFT, 100);
	m_ctrlNodeList.InsertColumn(5, _T("mem Free"), LVCFMT_LEFT, 100);
	m_ctrlNodeList.InsertColumn(6, _T("Request Count"), LVCFMT_LEFT, 100);
	m_ctrlNodeList.InsertColumn(7, _T("Last Req Time"), LVCFMT_LEFT, 200);

	m_ctrlNodeList.SetFont(&m_Font);

	{
		m_ImageList.Create ( 16 , 16 , ILC_MASK|ILC_COLOR32 , 12 , 0 ) ;


		m_ImageList.Add ( AfxGetApp()->LoadIcon(IDI_ICON1) );
		m_ImageList.Add ( AfxGetApp()->LoadIcon(IDI_ICON2) );
		m_ImageList.Add ( AfxGetApp()->LoadIcon(IDI_ICON3) );
		m_ImageList.Add ( AfxGetApp()->LoadIcon(IDI_ICON4) );

		m_ctrlNodeList.SetImageList ( &m_ImageList , LVSIL_SMALL ) ;
	}

	return 0;
}



void CNodeListPane::OnDestroy()
{
	CDockablePane::OnDestroy();
	m_ImageList.DeleteImageList() ;
}

void CNodeListPane::UpdateList ( CArray <SERVER_NODE,SERVER_NODE> &aNodes ) 
{
	m_ctrlNodeList.DeleteAllItems() ;

	
	for (int n=0;n<aNodes.GetCount();n++)
	{
		m_ctrlNodeList.InsertItem(n, aNodes[n].strIP ) ;
		CString strType = "Unknown" ;
		switch ( atol(aNodes[n].strType))
		{
		case 1:
			strType = "Web Server" ;
			break;
		case 2:
			strType = "Log Server" ;
			break;
		case 3:
			strType = "Load Balancer" ;
			break;
		case 4:
			strType = "GUI" ;
			break;
			
		}
		m_ctrlNodeList.SetItemText(n,1, strType) ;
		CString strCPU ;
		strCPU.Format( "%.1f %%", (float)atol(aNodes[n].strCPU) / 10);
		m_ctrlNodeList.SetItemText(n,2, strCPU ) ;
		m_ctrlNodeList.SetItemText(n,3, aNodes[n].strMemUsed ) ;
		m_ctrlNodeList.SetItemText(n,4, aNodes[n].strMemFree ) ;
		m_ctrlNodeList.SetItemText(n,5, aNodes[n].strRequestCount) ;
		m_ctrlNodeList.SetItemText(n,6, aNodes[n].strLastRequestTime ) ;
	}
}

BOOL CNodeListPane::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default

	return FALSE;//CDockablePane::OnEraseBkgnd(pDC);
}
